﻿using System;
using IWshRuntimeLibrary;
using System.Collections.Generic;   // 리스트 클래스 사용
using System.ComponentModel;
using System.Data;
using System.Drawing;   // 아이콘 클래스 사용
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace JJDock
{
    public partial class Form1 : Form//, IMessageFilter
    {
        private Point m_oMouseOffset;   // 폼 이동시 이용되는 포인터

        //모니터 크기를 구한다. 이 값으로 폴더 최초 위치를 구한다.
        private Rectangle m_oWorkingRectangle = Screen.PrimaryScreen.WorkingArea;
        private Point m_oCenterPointer;

        //폴더와 폴더좌표 멤버변수
        private Folder[] m_oFolder = new Folder[10];
        private Point m_oFolderPointer = new Point();

        //폴더 고정 좌표 배열
        private int[] m_nFolderPosX = {0, 160, 230, 160, 0, -150, -300, -370, -300, -150};
        private int[] m_nFolderPosY = {-350, -240, -70, 100, 230, 230, 100, -70, -240, -350};

        //폴더 이동 좌표 배열
        private int[] m_nFolderPosMoveX = {115, 275, 345, 275, 115, -35, -185, -255, -185, -35};
        private int[] m_nFolderPosMoveY = {-237, -127, 43, 213, 343, 343, 213, 43, -127, -237};
        
        //아이콘 객체 저장 멤버 변수
        private List<DesktopIcon> m_oDesktopIcon = new List<DesktopIcon>();
        public List<DesktopIcon> m_oDesktopIcon_SG
        {
            get
            {
                return m_oDesktopIcon;
            }
        }
        
        //아이콘 좌표변수
        private Point m_oIconPointer = new Point();

        //아이콘 고정좌표 체크
        //생성자에서만 고정좌표를 한번쓰기때문에 그 뒤부터는 이동좌표를 쓰도록 처리하기 위해 필요한 변수.
        private bool m_bIconPosCheck = false;

        //아이콘 고정 좌표 배열
        private int[] m_nIconPosX = { -30, 20, 70, 70, 70, 70, 70, 70, 20, -30, -90, -140, -190, -190, -190, -190, -190, -190, -140, -90 };
        private int[] m_nIconPosY = { -200, -200, -200, -130, -60, 10, 80, 150, 150, 150, 150, 150, 150, 80, 10, -60, -130, -200, -200, -200 };

        //아이콘 이동 좌표 배열
        private int[] m_nIconPosMoveX = { 85, 135, 185, 185, 185, 185, 185, 185, 135, 85, 25, -25, -75, -75, -75, -75, -75, -75, -25, 25 };
        private int[] m_nIconPosMoveY = { -87, -87, -87, -17, 53, 123, 193, 263, 263, 263, 263, 263, 263, 193, 123, 53, -17, -87, -87, -87 };

        //아이콘 경로 저장 리스트
        private List<String> m_oDesktopIconList = new List<String>();
        public List<String> m_oDesktopIconList_SG
        {
            get
            {
                return m_oDesktopIconList;
            }
        }
        
        //폴더/아이콘 보여주거나 숨겨주는 결정 멤버 변수
        private bool m_bShowHide = false;

        private StreamWriter writer;
        private StreamReader reader;
        private String m_sCenterImagePath = null;
      

        public Form1()
        {
            
            InitializeComponent();
            
            //모니터의 가운데 좌표값
            m_oCenterPointer = new Point(m_oWorkingRectangle.Width / 2, m_oWorkingRectangle.Height / 2);
            this.Location = m_oCenterPointer;
                        
            for (int i = 0; i < m_oFolder.Length; i++)
            {
                m_oFolder[i] = new Folder(i);
                m_oFolder[i].Show();
                m_oFolder[i].SetForm(this);
            }
            try
            {
                //이미지를 가져온다.
                reader = new StreamReader(Application.StartupPath + @"\System\ImagePath\CenterImagePath.dat");
                while (!reader.EndOfStream)
                {
                    m_sCenterImagePath = reader.ReadLine();                    
                }
                reader.Close();

                if (m_sCenterImagePath != null && System.IO.File.Exists(m_sCenterImagePath))
                {
                    m_oCenterImage.Load(m_sCenterImagePath);
                }

                //시작 폴더 값을 가져온다.
                reader = new StreamReader(Application.StartupPath + @"\System\SelectFolder.dat");
                while (!reader.EndOfStream)
                {
                    Folder.m_nSelectFolder_SG = Int32.Parse(reader.ReadLine());
                }
                reader.Close();

               
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

            FolderPosition();

            IconUpdate();

            //이 이후 부터는 이동 좌표가 불러와진다.
            m_bIconPosCheck = true;

            //고정좌표는 생성자에서만 한번 부른다.
            for (int i = 0; i < m_oDesktopIconList.Count; i++)
            {
                m_oIconPointer.X = this.Location.X + m_nIconPosX[i];
                m_oIconPointer.Y = this.Location.Y + m_nIconPosY[i];
                m_oDesktopIcon[i].Location = m_oIconPointer;
            }
            
        }

        // 다른 폴더가 클릭 될 때마다 해당 폴더의 아이콘을 불러와 보여준다.
        public void IconUpdate()
        {
            try
            {
                reader = new StreamReader(Application.StartupPath + @"\System\DesktopIconPath\FolderIconPath" + Folder.m_nSelectFolder_SG + ".dat");
                while (!reader.EndOfStream)
                {
                    m_oDesktopIconList.Add(reader.ReadLine());
                }
                reader.Close();

                
                bool Check = false;
                for (int i = 0; i < m_oDesktopIconList.Count; i++)
                {
                    if (!System.IO.File.Exists(m_oDesktopIconList[i]))
                    {
                        m_oDesktopIconList.RemoveAt(i);
                        Check = true;
                    }
                }

                if (Check)
                {
                    writer = new StreamWriter(Application.StartupPath + @"\System\DesktopIconPath\FolderIconPath" + Folder.m_nSelectFolder_SG + ".dat");
                    for (int i = 0; i < m_oDesktopIconList.Count; i++)
                    {
                        writer.WriteLine(m_oDesktopIconList[i]);
                    }
                    writer.Close();
                }
                
                for (int i = 0; i < m_oDesktopIconList.Count; i++)
                {
                    m_oDesktopIcon.Add(new DesktopIcon(m_oDesktopIconList[i]));
                    m_oDesktopIcon[i].setForm(this);
                    m_oDesktopIcon[i].Show();
                }

                if (m_bIconPosCheck)
                {
                    //아이콘 이동 좌표로 표시
                    for (int i = 0; i < m_oDesktopIconList.Count; i++)
                    {
                        m_oIconPointer.X = this.Location.X + m_nIconPosMoveX[i];
                        m_oIconPointer.Y = this.Location.Y + m_nIconPosMoveY[i];
                        m_oDesktopIcon[i].Location = m_oIconPointer;
                    }
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        // 폴더에서 드레그로 아이콘이 추가되었을 경우 처리되는 메소드 오버로딩됨.
        public void IconUpdate(String s)
        {
            try
            {
                reader = new StreamReader(Application.StartupPath + @"\System\DesktopIconPath\FolderIconPath" + Folder.m_nSelectFolder_SG + ".dat");
                while (!reader.EndOfStream)
                {
                    m_oDesktopIconList.Add(reader.ReadLine());
                }
                reader.Close();

                if ((System.IO.File.GetAttributes(s) & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    DirectoryInfo dir = new DirectoryInfo(s);
                    if (!System.IO.File.Exists(Application.StartupPath + @"\System\FolderLink\" + dir.Name + ".lnk"))
                    {
                        WshShellClass WshShell = new WshShellClass();
                        IWshRuntimeLibrary.IWshShortcut MyShortcut;
                        MyShortcut = (IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(Application.StartupPath + @"\System\FolderLink\" + dir.Name + ".lnk");
                        MyShortcut.TargetPath = s;
                        MyShortcut.Description = "폴더 바로가기 파일 입니다.";
                        MyShortcut.IconLocation = Application.StartupPath + @"\System\FolderIcon.ico";
                        MyShortcut.Save();
                    }

                    m_oDesktopIconList.Add(Application.StartupPath + @"\System\FolderLink\" + dir.Name + ".lnk");
                }
                else
                {
                    m_oDesktopIconList.Add(s);
                }
                

                for (int i = 0; i < m_oDesktopIconList.Count; i++)
                {
                    if (!System.IO.File.Exists(m_oDesktopIconList[i]))
                    {
                        m_oDesktopIconList.RemoveAt(i);
                    }
                }
                writer = new StreamWriter(Application.StartupPath + @"\System\DesktopIconPath\FolderIconPath" + Folder.m_nSelectFolder_SG + ".dat");
                for (int i = 0; i < m_oDesktopIconList.Count; i++)
                {
                    writer.WriteLine(m_oDesktopIconList[i]);
                }
                writer.Close();

                for (int i = 0; i < m_oDesktopIconList.Count; i++)
                {
                    m_oDesktopIcon.Add(new DesktopIcon(m_oDesktopIconList[i]));
                    m_oDesktopIcon[i].setForm(this);
                    m_oDesktopIcon[i].Show();
                }

                if (m_bIconPosCheck)
                {
                    for (int i = 0; i < m_oDesktopIconList.Count; i++)
                    {
                        m_oIconPointer.X = this.Location.X + m_nIconPosMoveX[i];
                        m_oIconPointer.Y = this.Location.Y + m_nIconPosMoveY[i];
                        m_oDesktopIcon[i].Location = m_oIconPointer;
                    }
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        public void FolderPosition()
        {
            for (int i = 0; i < m_oFolder.Length; i++)
            {
                m_oFolderPointer.X = this.Location.X + m_nFolderPosX[i];
                m_oFolderPointer.Y = this.Location.Y + m_nFolderPosY[i];
                m_oFolder[i].Location = m_oFolderPointer;
            }

        }

        public void FolderPosition(Point pt)
        {
            //폴더 이동좌표
            Point[] ptemp = new Point[10];
            for (int i = 0; i < 10; i++)
            {
                ptemp[i] = pt;
            }

            //아이콘 이동좌표
            Point[] pitem = new Point[20];
            for (int i = 0; i < 20; i++)
            {
                pitem[i] = pt;
            }

            //폴더 움직일때 좌표 처리
            for (int i = 0; i < m_oFolder.Length; i++)
            {
                m_oFolderPointer.X = m_oMouseOffset.X + m_nFolderPosMoveX[i];
                m_oFolderPointer.Y = m_oMouseOffset.Y + m_nFolderPosMoveY[i];
                ptemp[i].Offset(m_oFolderPointer.X - 12, m_oFolderPointer.Y - 12);
                m_oFolder[i].Location = ptemp[i];
                m_oFolder[i].Activate();
            }

            //아이콘 움직일때 좌표 처리
            for (int i = 0; i < m_oDesktopIconList.Count; i++)
            {
                m_oIconPointer.X = m_oMouseOffset.X + m_nIconPosMoveX[i];
                m_oIconPointer.Y = m_oMouseOffset.Y + m_nIconPosMoveY[i];
                pitem[i].Offset(m_oIconPointer.X - 12, m_oIconPointer.Y - 12);
                m_oDesktopIcon[i].Location = pitem[i];
                m_oDesktopIcon[i].Activate();
            }
        }

        private void m_oToolStripMenuItem_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void m_oCenterImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)  // 왼쪽 마우스를 클릭했는지 체크
            {
                Point mousePos = Control.MousePosition; // 마우스 커서의 위치 값을 가져온다.
                
                FolderPosition(mousePos);
                
                mousePos.Offset(m_oMouseOffset.X-12, m_oMouseOffset.Y-12);    // 최초 위치값에서 이동된 위치값 만큼 값을 변환 한다.
                
                Location = mousePos;    // 현재 객체의 위치값을 바꾼다.
                
            }
        }

        private void m_oCenterImage_MouseDown(object sender, MouseEventArgs e)
        {
            m_oMouseOffset = new Point(-e.X, -e.Y); // 최초 위치값을 가져온다.
        }

        private void m_oToolStripMenuItem_CenterImage_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_oOpenFileDialog_CenterImage.ShowDialog() == DialogResult.OK)
                {
                    m_oCenterImage.Load(m_oOpenFileDialog_CenterImage.FileName);

                    if (System.IO.File.Exists(m_oOpenFileDialog_CenterImage.FileName))
                    {
                        writer = new StreamWriter(Application.StartupPath + @"\System\ImagePath\CenterImagePath.dat");
                        writer.WriteLine(m_oOpenFileDialog_CenterImage.FileName);
                        writer.Close();
                    }
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show("파일 또는 경로가 잘 못 되었습니다.\n" + ee.ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
            timer1.Interval = 100;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Program.ScreenCheck)
            {
                for (int i = 0; i < m_oFolder.Length; i++)
                {
                    m_oFolder[i].Activate();
                }
                for (int i = 0; i < m_oDesktopIconList.Count; i++)
                {
                    m_oDesktopIcon[i].Activate();
                }
                this.Activate();
            }
        }

        private void m_oCenterImage_DoubleClick(object sender, EventArgs e)
        {
            if (m_bShowHide)
            {
                m_bShowHide = false;
                for (int i = 0; i < m_oFolder.Length; i++)
                {
                    m_oFolder[i].Show();
                }
                for (int i = 0; i < m_oDesktopIconList.Count; i++)
                {
                    m_oDesktopIcon[i].Show();
                }
            }
            else
            {
                m_bShowHide = true;
                for (int i = 0; i < m_oFolder.Length; i++)
                {
                    m_oFolder[i].Hide();
                }
                for (int i = 0; i < m_oDesktopIconList.Count; i++)
                {
                    m_oDesktopIcon[i].Hide();
                }
            }
        }

        private void m_oToolStripMenuItem_Help_Click(object sender, EventArgs e)
        {
            Help help = new Help();
            help.ShowDialog();
        }

        private void m_oToolStripMenuItem_IconAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_oOpenFileDialog_IconAdd.ShowDialog() == DialogResult.OK)
                {
                    if (System.IO.File.Exists(m_oOpenFileDialog_IconAdd.FileName) && m_oDesktopIconList.Count < 20)
                    {
                        m_oDesktopIconList.Add(m_oOpenFileDialog_IconAdd.FileName);

                        writer = new StreamWriter(Application.StartupPath + @"\System\DesktopIconPath\FolderIconPath" + Folder.m_nSelectFolder_SG + ".dat");
                        for (int i = 0; i < m_oDesktopIconList.Count; i++)
                        {
                            writer.WriteLine(m_oDesktopIconList[i]);
                        }
                        writer.Close();

                        for (int i = 0; i < m_oDesktopIcon.Count; i++)
                        {
                            m_oDesktopIcon[i].Dispose();
                        }

                        m_oDesktopIcon.Clear(); 
                        m_oDesktopIconList.Clear();

                        IconUpdate();
                    }
                    else
                    {
                        MessageBox.Show("파일이 없거나 아이콘 20개를 초과 등록하였습니다.");
                    }
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show("파일 또는 경로가 잘 못 되었습니다.\n" + ee.ToString());
            }
        }

    }

}
